﻿class com.thaiflashdev.utils.WmodeFix
{
	/**
	 * @example
	 * WmodeFix.fix(textfield);
	 * 
	 * @author	keerotic
	 */	
	public static function fix(ti:TextField) 
	{
		ti.onChanged = function()
		{
			var str:String = this.text;
			var cursor:Number = Selection.getEndIndex();
			var len:Number = str.length;
			if (len > 0) 
			{
				for (var i:Number = 0; i< len; i++) 
				{
					var code1:Number = str.charCodeAt(i);
					if (code1 == 14) 
					{
						var dropChar:String = str.charAt(i);
						var code2:Number = str.charCodeAt(i+1);
						var newCode:Number;
						
						if (isNaN(code2)) 
						{ 
							newCode = (code1 << 8)+13;
						} 
						else 
						{
							i++;
							cursor--;
							dropChar += str.charAt(i);
							
							if (code2 == 13) 
							{ 
								newCode = (code1 << 8)+10;
							} 
							else 
							{
								newCode = (code1 << 8)+code2;
							}
						}
						var newChar:String = String.fromCharCode(newCode);
						var tempArray:Array = str.split(dropChar);
						str = tempArray.join(newChar);
					}
				}
			}
			this.text = str;
			Selection.setSelection(cursor,cursor);
		}
	}
}